home *** CD-ROM | disk | FTP | other *** search
/ Xentax forum attachments archive / xentax.7z / 7236 / Natural Doctrine.7z / Natural Doctrine.ms
Encoding:
Text File  |  2014-04-20  |  6.5 KB  |  273 lines

  1. if (heapSize < 200000) then
  2.         heapSize = 2000000 -- allow ~ 40 MB instead of just 7.5 MB. Prevents "Runtime Error: Out of scripter memory"
  3.  
  4. fname = getOpenFileName \
  5. caption:"Open .scn from Mesh folder" \
  6. types:"Natural Doctrine  (*.scn)|*.scn" \
  7. historyCategory:"NaturalDoctrineObjectPresets"
  8. f = fopen fname "rb"
  9.  
  10. clearlistener()
  11. fn readHalfFloat fstream = (
  12.     hf=readshort fstream #unsigned
  13.     sign = bit.get hf 16
  14.     exponent = (bit.shift (bit.and hf (bit.hexasint "7C00")) -10) as integer - 16
  15.     fraction = bit.and hf (bit.hexasint "03FF")
  16.     if sign==true then sign = 1 else sign = 0
  17.     exponentF = exponent + 127
  18.     outputAsFloat = bit.or (bit.or (bit.shift fraction 13) \
  19.     (bit.shift exponentF 23)) (bit.shift sign 31)
  20.     return bit.intasfloat outputasfloat*2
  21.     )
  22. fn readFixedString bstream fixedLen = (
  23. local str = ""
  24. for i = 1 to fixedLen do (
  25. str += bit.intAsChar (ReadByte bstream #unsigned))
  26. str
  27. )
  28. struct weight_data
  29. (
  30.     boneids,weights
  31. )
  32. clearlistener()
  33.  
  34. fseek f 0x0 #seek_set
  35. SCN=readFixedString f 4
  36. MathPathCount = Readlong f
  37. MathCount = Readlong f
  38. MathOff = Readlong f
  39. MCount = Readlong f
  40. MathSecSize = Readlong f
  41. BoneCount = Readlong f
  42. BoneOff = Readlong f
  43. MeshCount = Readlong f
  44. MeshOff = Readlong f
  45.  
  46. BNArr = #()
  47. MeshName = #()
  48. HT = dotNetObject "System.Collections.Hashtable"
  49. BoneParent = #()
  50. BoneTable = #()
  51. BoneNames = #()
  52.  
  53. fseek f BoneOff #seek_set
  54.  
  55. for a = 1 to (BoneCount-MeshCount) do (
  56.  
  57. c11 = ReadFloat f; c12 = ReadFloat f; c13 = ReadFloat f; c14 = ReadFloat f    
  58. c21 = ReadFloat f; c22 = ReadFloat f; c23 = ReadFloat f; c24 = ReadFloat f    
  59. c31 = ReadFloat f; c32 = ReadFloat f; c33 = ReadFloat f; c34 = ReadFloat f
  60. c41 = ReadFloat f; c42 = ReadFloat f; c43 = ReadFloat f; c44 = ReadFloat f
  61.  
  62.             
  63. BoneName=readFixedString f 32
  64.     
  65. fseek f 0x160 #seek_cur
  66.     
  67. Unk1 = Readlong f    
  68. BoneHashId = (Readlong f #unsigned) as string    
  69. BoneId = Readlong f    
  70. BoneParent = Readlong f #unsigned        
  71. LongCount = Readlong f
  72. Unk2 = Readlong f        
  73. for x = 1 to LongCount do (
  74. BoneChild = Readlong f  
  75. )    
  76.  
  77. HT.Add (BoneHashId) BoneName 
  78.  
  79. tfm = matrix3 [c11,c12,c13] [c21,c22,c23] [c31,c32,c33] ([c41,c42,c43]*100)
  80.     
  81. newBone = bonesys.createbone    \
  82.                   tfm.row4    \
  83.                   (tfm.row4 + 0.01 * (normalize tfm.row1)) \
  84.                   (normalize tfm.row3)
  85.             newBone.name = BoneName
  86.             newBone.width  = 0.01
  87.             newBone.height = 0.01
  88.             newBone.transform = tfm
  89.             newBone.setBoneEnable false 0
  90.             newBone.wirecolor = white
  91.             newbone.showlinks = true
  92.             newBone.pos.controller      = TCB_position ()
  93.             newBone.rotation.controller = TCB_rotation ()                  
  94. append BNArr newBone                     
  95. )
  96.  
  97. for a = 1 to MeshCount do (
  98.  
  99. fseek f 0x40 #seek_cur
  100.             
  101. append MeshName (readFixedString f 32)
  102.  
  103. fseek f 0x160 #seek_cur
  104.     
  105. Unk1 = Readlong f    
  106. Unk2 = Readlong f #unsigned    
  107. Unk3 = Readlong f    
  108. Unk4 = Readlong f    
  109. CountLong = Readlong f    
  110.     
  111. for x = 1 to CountLong do (
  112. Long = Readlong f  
  113. )    
  114. Unk5 = Readlong f #unsigned                  
  115. )
  116.  
  117. Print ("Bone End @ 0x"+((bit.intAsHex(ftell f))as string))
  118.  
  119. fseek f MeshOff #seek_set
  120.  
  121. Print ("Mesh Off @ 0x"+((bit.intAsHex(ftell f))as string))
  122.  
  123. for i = 1 to MeshCount do (    
  124.     
  125. VertexCount = Readlong f
  126. FaceCount = Readlong f
  127. VertexId = Readlong f
  128. UCount = Readlong f
  129. Unk1 = ReadFloat f
  130. UsedBoneCount = Readlong f
  131. Unk2 = ReadFloat f
  132.  
  133. fseek f 0x60 #seek_cur
  134.  
  135. vertArray = #()
  136. Facearray = #()    
  137. UV_array = #()
  138. Weight_array =#()
  139. BoneMap =#()    
  140.  
  141. Print ("Vertex Start @ 0x"+((bit.intAsHex(ftell f))as string))
  142.     
  143. for x = 1 to VertexCount do (
  144.     
  145. getPos = ftell f + 44        
  146.  
  147. vx = Readfloat f
  148. vy = Readfloat f
  149. vz = Readfloat f
  150.     
  151. fseek f 0xC #seek_cur    
  152.  
  153. tu=readHalfFloat f    
  154. tv=readHalfFloat f*-1    
  155.     
  156. fseek f 0x8 #seek_cur        
  157.     
  158. weight1 = readbyte f #unsigned    
  159. weight2 = readbyte f #unsigned    
  160. weight3 = readbyte f #unsigned    
  161. weight4 = readbyte f #unsigned    
  162.     
  163. bone1 = readbyte f #unsigned    
  164. bone2 = readbyte f #unsigned    
  165. bone3 = readbyte f #unsigned    
  166. bone4 = readbyte f #unsigned
  167.     
  168. w = (weight_data boneids:#() weights:#())
  169. maxweight = 0
  170. if(weight1 != 0) then
  171.    maxweight = maxweight + weight1
  172. if(weight2 != 0) then
  173.    maxweight = maxweight + weight2
  174. if(weight3 != 0) then
  175.    maxweight = maxweight + weight3
  176. if(weight4 != 0) then
  177.    maxweight = maxweight + weight4
  178.  
  179.  
  180. if(maxweight != 0) then (
  181.       if(weight1 != 0) then (
  182.          w1 = weight1 as float
  183.          append w.boneids (bone1 + 1)
  184.          append w.weights (w1/255.0)
  185.       )
  186.       if(weight2 != 0) then (
  187.          w2 = weight2 as float
  188.          append w.boneids (bone2 + 1)
  189.          append w.weights (w2/255.0)
  190.       )
  191.       if(weight3 != 0) then (
  192.          w3 = weight3 as float
  193.          append w.boneids (bone3 + 1)
  194.          append w.weights (w3/255.0)
  195.       )
  196.       if(weight4 != 0) then (
  197.          w4 = weight4 as float
  198.          append w.boneids (bone4 + 1)
  199.         append w.weights (w4/255.0)
  200.       )      
  201.    )
  202. fseek f getPos #seek_set 
  203. append Weight_array w               
  204. append vertArray [vx,vy,vz]
  205. append UV_array [tu,tv,0]     
  206. )    
  207.  
  208. Print ("Vertex End @ 0x"+((bit.intAsHex(ftell f))as string))
  209.  
  210. for x = 1 to FaceCount/3 do (
  211. f1 = (Readlong f #unsigned + 1)
  212. f2 = (Readlong f #unsigned + 1) 
  213. f3 = (Readlong f #unsigned + 1) 
  214. append Facearray [f1,f2,f3]
  215. )
  216.  
  217. Print ("Face End @ 0x"+((bit.intAsHex(ftell f))as string))
  218.     
  219. for i = 1 to UsedBoneCount do (
  220. bonehash = (readlong f #unsigned) as string    
  221. append BoneMap bonehash     
  222. )
  223.  
  224. for a = 1 to UsedBoneCount do (    
  225. getPos = ftell f + 64            
  226. fseek f getPos #seek_set    
  227. )    
  228.  
  229. Print ("Section End @ 0x"+((bit.intAsHex(ftell f))as string))
  230.  
  231. msh = mesh vertices:vertArray faces:faceArray
  232. msh.name=MeshName[i]
  233. msh.numTVerts = UV_array.count
  234. buildTVFaces msh
  235. for j = 1 to UV_array.count do setTVert msh j UV_array[j]
  236. for j = 1 to faceArray.count do setTVFace msh j faceArray[j]
  237. msh.displayByLayer = false
  238. msh.backfacecull = true
  239. msh.wirecolor = (color 230 200 210)
  240.  
  241. max modify mode
  242. select msh
  243. skinMod = skin ()
  244. addModifier msh skinMod
  245.  
  246. for i = 1 to UsedBoneCount do
  247. (
  248.     maxbone = getnodebyname HT.item[BoneMap[i]]
  249.     if i != UsedBoneCount then 
  250.         skinOps.addBone skinMod maxbone 0
  251.     else
  252.         skinOps.addBone skinMod maxbone 1
  253. )
  254. modPanel.setCurrentObject skinMod
  255. for i = 1 to VertexCount do (
  256.    w = Weight_array[i]
  257.    bi = #() --bone index array
  258.    wv = #() --weight value array
  259.    
  260.    for j = 1 to w.boneids.count do
  261.    (
  262.       boneid = w.boneids[j]
  263.       weight = w.weights[j]
  264.       append bi boneid
  265.       append wv weight
  266.    )   
  267.    
  268.    skinOps.ReplaceVertexWeights skinMod i bi wv
  269.    
  270. )
  271. )
  272.  
  273. fclose f